"use strict";

/*
 * MikoPBX - free phone system for small business
 * Copyright © 2017-2023 Alexey Portnov and Nikolay Beketov
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program.
 * If not, see <https://www.gnu.org/licenses/>.
 */

/* global moment */

/**
 * Object for managing the clock worker.
 *
 * @module clockWorker
 */
var clockWorker = {
  /**
   * The id of the timer function for the worker.
   * @type {number}
   */
  timeOutHandle: 0,

  /**
   * Options for the clock worker.
   * @type {object|null}
   */
  options: null,

  /**
   * Initializes the clock worker.
   */
  initialize: function initialize() {
    clockWorker.restartWorker();
  },

  /**
   * Restarts the clock worker.
   */
  restartWorker: function restartWorker() {
    window.clearTimeout(clockWorker.timeoutHandle);
    clockWorker.worker();
  },

  /**
   * Performs the clock worker operations.
   */
  worker: function worker() {
    PbxApi.GetDateTime(clockWorker.cbAfterReceiveDateTimeFromServer);
  },

  /**
   * Callback function after receiving the date and time from the server.
   * @param {object|boolean} response - The response from the server.
   */
  cbAfterReceiveDateTimeFromServer: function cbAfterReceiveDateTimeFromServer(response) {
    var options = {
      timeZone: timeSettings.$formObj.form('get value', 'PBXTimezone'),
      timeZoneName: 'short'
    };

    if (timeSettings.$formObj.form('get value', 'PBXManualTimeSettings') !== 'on') {
      clockWorker.timeoutHandle = window.setTimeout(clockWorker.worker, 1000);
    } else {
      options.timeZoneName = undefined;
    }

    if (response !== false) {
      var dateTime = new Date(response.timestamp * 1000);
      moment.locale(globalWebAdminLanguage);
      var m = moment(dateTime); //timeSettings.$formObj.form('set value', 'ManualDateTime', dateTime.toLocaleString(globalWebAdminLanguage, options));

      timeSettings.$formObj.form('set value', 'ManualDateTime', m.tz(options.timeZone).format());
    }
  }
}; // When the document is ready, initialize the time settings worker

$(document).ready(function () {
  clockWorker.initialize();
});
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9UaW1lU2V0dGluZ3MvdGltZS1zZXR0aW5ncy13b3JrZXIuanMiXSwibmFtZXMiOlsiY2xvY2tXb3JrZXIiLCJ0aW1lT3V0SGFuZGxlIiwib3B0aW9ucyIsImluaXRpYWxpemUiLCJyZXN0YXJ0V29ya2VyIiwid2luZG93IiwiY2xlYXJUaW1lb3V0IiwidGltZW91dEhhbmRsZSIsIndvcmtlciIsIlBieEFwaSIsIkdldERhdGVUaW1lIiwiY2JBZnRlclJlY2VpdmVEYXRlVGltZUZyb21TZXJ2ZXIiLCJyZXNwb25zZSIsInRpbWVab25lIiwidGltZVNldHRpbmdzIiwiJGZvcm1PYmoiLCJmb3JtIiwidGltZVpvbmVOYW1lIiwic2V0VGltZW91dCIsInVuZGVmaW5lZCIsImRhdGVUaW1lIiwiRGF0ZSIsInRpbWVzdGFtcCIsIm1vbWVudCIsImxvY2FsZSIsImdsb2JhbFdlYkFkbWluTGFuZ3VhZ2UiLCJtIiwidHoiLCJmb3JtYXQiLCIkIiwiZG9jdW1lbnQiLCJyZWFkeSJdLCJtYXBwaW5ncyI6Ijs7QUFBQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBOztBQUVBOztBQUdBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQSxJQUFNQSxXQUFXLEdBQUc7QUFDaEI7QUFDSjtBQUNBO0FBQ0E7QUFDSUMsRUFBQUEsYUFBYSxFQUFFLENBTEM7O0FBTWhCO0FBQ0o7QUFDQTtBQUNBO0FBQ0lDLEVBQUFBLE9BQU8sRUFBRSxJQVZPOztBQVloQjt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